package cn.stylefeng.guns.modular.system.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.guns.core.common.node.TreeviewNode;
import cn.stylefeng.guns.core.common.node.ZTreeNode;
import cn.stylefeng.guns.fremework.BaseController;
import cn.stylefeng.guns.fremework.ResponseData;
import cn.stylefeng.guns.modular.system.entity.SysDept;
import cn.stylefeng.guns.modular.system.model.DeptDto;
import cn.stylefeng.guns.modular.system.service.SysDeptService;
import cn.stylefeng.guns.modular.system.warpper.DeptTreeWrapper;
import cn.stylefeng.guns.modular.system.warpper.DeptWrapper;
import cn.stylefeng.guns.fremework.LayuiPageFactory;
import cn.stylefeng.roses.core.treebuild.DefaultTreeBuildFactory;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/sysDept")
public class SysDeptController extends BaseController {

    private String PREFIX = "/modular/system/sysDept/";

    @Autowired
    private SysDeptService deptService;

    ///////////////////////分页///////////////////////
    @RequestMapping("")
    public String index() {
        return PREFIX + "sysDept.html";
    }


    @ResponseBody
    @RequestMapping(value = "/page")
    public ResponseData page(Page page, SysDept params) {
        LambdaQueryWrapper<SysDept> queryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(params.getSimpleName())){
            queryWrapper.and(wrapper -> wrapper.like(SysDept::getSimpleName, params.getSimpleName()));
        }
        if (!StringUtils.isEmpty(params.getDeptId())){
            queryWrapper.and(wrapper -> wrapper.like(SysDept::getDeptId, params.getDeptId()));
        }
        IPage result = deptService.page(page, queryWrapper);
        return ResponseData.success(result);
    }

    ///////////////////////新增///////////////////////
    @RequestMapping("/sysDeptAdd")
    public String sysDeptAdd() {
        return PREFIX + "sysDeptAdd.html";
    }

    @ResponseBody
    @RequestMapping(value = "/save")
    public ResponseData save(SysDept dept) {
        deptService.save(dept);
        return ResponseData.success();
    }
    ///////////////////////详情///////////////////////
    @ResponseBody
    @RequestMapping(value = "/detail/{deptId}")
    public Object detail(@PathVariable("deptId") Long deptId) {
        SysDept dept = deptService.getById(deptId);
        DeptDto deptDto = new DeptDto();
        BeanUtil.copyProperties(dept, deptDto);
        SysDept sysDeptParent = deptService.getById(deptDto.getPid());
        deptDto.setPName(sysDeptParent.getSimpleName());
        return deptDto;
    }

    ///////////////////////修改///////////////////////
    @RequestMapping("/sysDeptEdit")
    public String sysDeptEdit() {
        return PREFIX + "sysDeptEdit.html";
    }

    @ResponseBody
    @RequestMapping(value = "/update")
    public ResponseData update(SysDept dept) {
//        deptService.editDept(dept);
        deptService.updateById(dept);
        return ResponseData.success();
    }
    ///////////////////////删除///////////////////////
    @ResponseBody
    @RequestMapping(value = "/delete/{deptId}")
    public ResponseData delete(@PathVariable("deptId") Long deptId) {
        deptService.removeById(deptId);
        return ResponseData.success();
    }

    /**
     * 获取部门的tree列表，ztree格式
     *
     * @author fengshuonan
     * @Date 2018/12/23 4:56 PM
     */
    @ResponseBody
    @RequestMapping(value = "/tree")
    public List<ZTreeNode> tree() {
        List<ZTreeNode> tree = this.deptService.tree();
//        tree.add(ZTreeNode.createParent());
        return tree;
    }
    /**
     * 获取部门的tree列表，treeview格式
     *
     * @author fengshuonan
     * @Date 2018/12/23 4:57 PM
     */
    @ResponseBody
    @RequestMapping(value = "/treeview")
    public List<TreeviewNode> treeview() {
        List<TreeviewNode> treeviewNodes = this.deptService.treeviewNodes();

        //构建树
        DefaultTreeBuildFactory<TreeviewNode> factory = new DefaultTreeBuildFactory<>();
        factory.setRootParentId("0");
        List<TreeviewNode> results = factory.doTreeBuild(treeviewNodes);

        //把子节点为空的设为null
        DeptTreeWrapper.clearNull(results);

        return results;
    }

    @ResponseBody
    @RequestMapping(value = "/list")
    public Object list(@RequestParam(value = "condition", required = false) String condition,
                       @RequestParam(value = "deptId", required = false) Long deptId) {
        Page<Map<String, Object>> list = this.deptService.list(condition, deptId);
        Page<Map<String, Object>> wrap = new DeptWrapper(list).wrap();
        return LayuiPageFactory.createPageInfo(wrap);
    }



}
